//
//  ViewController.m
//  08- HomeWork
//
//  Created by dllo on 16/7/25.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#import "DetailViewController.h"
#import "AddStudentViewController.h"
#define kTableViewCellReuse @"reuse"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)NSMutableArray *dataArray;
@property (nonatomic, strong)UITableView *tableView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self loadData];
    
    self.navigationItem.title = @"通信录";
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addStudent)];
    
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
   _tableView.dataSource = self;
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kTableViewCellReuse];
    
    self.navigationItem.leftBarButtonItem = self.editButtonItem;
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    [self.tableView setEditing:editing animated:animated];
}


#pragma mark - 页面跳转传值
- (void)addStudent {
    AddStudentViewController *addStuVC = [[AddStudentViewController alloc] init];
//  5. 签订代理人
    addStuVC.delegate = self;
    
    [self.navigationController pushViewController:addStuVC animated:YES];
}

// 6. 实现协议方法
- (void)AddStudentWithDetailDictionary:(NSDictionary *)dictionary {
    
    [_dataArray addObject:dictionary];
    [_tableView reloadData];
}


#pragma mark - delegate
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    DetailViewController *detailVC = [[DetailViewController alloc] init];
    NSDictionary *perDictionary = [_dataArray objectAtIndex:indexPath.row];
    detailVC.dictionary = perDictionary;
    [self.navigationController pushViewController:detailVC animated:YES];
}

#pragma mark - dataSource
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
         NSLog(@"删除");
        [_dataArray removeObject:_dataArray[indexPath.row]];
        
        [_tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        NSLog(@"添加");
    }
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewCellReuse];
    
    cell.textLabel.text = [[_dataArray objectAtIndex:indexPath.row] objectForKey:@"name"];
    return cell;
}

#pragma mark - loadData(加载数据)
- (void)loadData {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"DLS160710" ofType:@"plist"];
    self.dataArray = [NSMutableArray arrayWithContentsOfFile:path];
   
    NSLog(@"%@",_dataArray);
}

#pragma mark - MemoryWarning
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
